package com.shengyin.ec.service.permission;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shengyin.ec.model.dao.permission.RolesDao;
import com.shengyin.ec.model.entity.Roles;

@Service(value="rolesService")
public class RolesServiceImpl implements RolesService {

	@Autowired
	RolesDao rolesDao;
	
	
	@Override
	public void createRoles(Roles roles) {
		rolesDao.save(roles);		
	}

	@Override
	public void deleteRoles(Roles roles) {
		Roles users=rolesDao.get(roles.getId());
		rolesDao.delete(users);
		
	}

	@Override
	public void deleteRolesById(long id) {
		rolesDao.deleteByKey(id);		
	}

	@Override
	public void modifyRoles(Roles roles) {
		
		Roles users=rolesDao.get(roles.getId());
		try {
			BeanUtils.copyProperties(users, roles);
			rolesDao.update(users);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}		
	}

	@Override
	public Roles getRolesById(long id) {
		return rolesDao.get(id);
	}

	@Override
	public List<Roles> getRolesList() {
		return rolesDao.loadAll();
	}

	@Override
	public List<Roles> getRolesList(int start, int end) {
		return rolesDao.getRolesList(start,end);
	}

	@Override
	public List<Roles> getRolesListByKeyword(String keyword, int start, int end) {
		return rolesDao.getRolesListByKeyword(keyword, start, end);
	}

	@Override
	public void deleteSelectedRoles(Long[] ids) {
		rolesDao.deleteSelectedRoles(ids);		
	}

	@Override
	public Long getTotal() {
		return  rolesDao.getTotal();		
	}

	@Override
	public void ResourcesAllocateToRole(String[] ids, long roleId) {
		System.out.println("================"+roleId);
		if(ids!=null){
			Long[] idsl=new Long[ids.length];
			for(int i=0;i<ids.length;i++){
				if(ids[i]!=null||!ids[i].isEmpty()){
					idsl[i]=Long.valueOf(ids[i].trim());
					System.out.println(idsl[i]+"=====ids======");
				}
				
			}
			
			rolesDao.ResourcesAllocateToRole(idsl, roleId);	
			}
			
	}

	@Override
	public void CancelRolesResources(long resourceid,long roleid) {
		// TODO Auto-generated method stub
		
	}

}
